package org.linlinjava.litemall.admin.manage.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.admin.beans.vo.LumierePayMemberVo;
import org.linlinjava.litemall.admin.manage.AdminLumierePayMemberManageService;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.LumierePayMember;
import org.linlinjava.litemall.db.domain.LumierePayMemberUser;
import org.linlinjava.litemall.db.domain.LumierePayMemberUserExample;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.service.LumierePayMemberService;
import org.linlinjava.litemall.db.service.LumierePayMemberUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class AdminLumierePayMemberManageServiceImpl implements AdminLumierePayMemberManageService {


    private final LumierePayMemberUserService lumierePayMemberUserService;

    private final LitemallUserService litemallUserService;

    private final LumierePayMemberService lumierePayMemberService;


    /***
     * 获取付费会员使用中的人数
     * @param memberId
     * @return
     */
    @Override
    public Integer getCurrentUsedPayMemberUserNumber(Integer memberId) {

        LumierePayMember payMember = lumierePayMemberService.findById(memberId);

        //获得当前付费会员的所有用户，并过滤没有过期的用户且没有注销的用户
        int currentNumbers = lumierePayMemberUserService.list(Wrappers.lambdaQuery(LumierePayMemberUser.class)
                        .eq(LumierePayMemberUser::getPayMemberId, memberId)
                ).stream()
                .filter(payMemberUser ->
                        payMemberUser.getEndTime().isAfter(LocalDateTime.now())&& ObjectUtil.isNotNull(litemallUserService.getOne(Wrappers.lambdaQuery(LitemallUser.class)
                                .eq(LitemallUser::getId,payMemberUser.getUserId())
                                .eq(LitemallUser::getDeleted,0))))
                .collect(Collectors.toList()).size();
        return payMember.getStatus().intValue()!=3?currentNumbers:0;
    }
}
